const { ccclass, property } = cc._decorator;

import c from "./AbTestMgr"
import u from "./OutsideGuideExperimentMgr"
import p from "./PeripheralSystemDataMgr"
import d from "./EventMgr"
import h from "./MatchGameEventType"
import f from "./frameworkManager"
import g from "./NodePool"
import y from "./AdManager"
import m from "./SdkHelper"
import _ from "./EngineUtil"
import v from "./MathUtil"
import b, { MatchGameStatus, SkillType } from "./MatchGameData"
import C from "./MatchGameGuideData"
import T from "./MatchGameConfig"
import M from "./MatchGameAudioMgr"
import P from "./MatchGameDataMgr"
import S from "./MatchGameLogicDataCtrl"
import w from "./PageMgr"
import A from "./MatchGameSystemMgr"
import O, { VideoBuriedPoint } from "./PeripheralSystemMgr"
import I from "./UiManage"
import N from "./GameDataMgr"
import E from "./MatchGameView"
import G from "./SkillAniCtrl"
import k from "./TargetAniCtrl"
import D from "./MatchGameCenterCtrl"
import UiManager from "./UiManage";
import MatchGameConfig from "./MatchGameConfig";
import MathUtils from "./MathUtil";
import NativeUtils from "./NativeUtils";

export enum PandaStatu {
    thinking = 1,
    Idle = 2,
    Elimate = 3,
    victory = 4,
    Fail = 5
}

export enum s {
    thinking = 1,
    Idle = 2,
    Elimate = 3,
    victory = 4,
    Fail = 5
}

@ccclass
export default class MatchGameViewCtrl extends cc.Component {

    public targetItems = [];
    public levelInfo = null;
    public targetItemPrefab = null;
    public readySkillType = SkillType.Null;
    public isFinish = false;
    public druingloadAni = false;
    public matchGameCenterInitFinished = false;
    public flyBoxPrefab = null;
    public pandaStatu = null;
    public ui;

    static instance: MatchGameViewCtrl;
    public async onLoad() {
        MatchGameViewCtrl.instance = this;
        this.onUILoad();
        this.addButtonListen();
        this.addEventListen();
        this.initTestBtnShow();


        // var d = await UiManager.loaderPrefabInDeepPath("prefabs/LevelEditor");
        // this.ui.node.addChild(cc.instantiate(d));
    };
    public start() { };
    public onUILoad() {
        this.ui = this.node.addComponent(E);
    };
    public initTestBtnShow() {
        this.ui.btn_test_gameFail.active = b.isEnableDebug();
        this.ui.btn_test_gamesuccess.active = cc.sys.isBrowser;//b.isEnableDebug();
        this.ui.btn_test_report.active = b.isEnableDebug();
    };
    public addButtonListen() {
        UiManager.addButtonListen(this.ui.btn_arrow, this.onArrow, this);
        UiManager.addButtonListen(this.ui.btn_hammer, this.onHammer, this);
        UiManager.addButtonListen(this.ui.btn_gun, this.onGun, this);
        UiManager.addButtonListen(this.ui.btn_shiff, this.onShiff, this);
        UiManager.addButtonListen(this.ui.btn_test_gameFail, this.onTestFail, this);
        UiManager.addButtonListen(this.ui.btn_test_gamesuccess, this.onTestSuccess, this);
        UiManager.addButtonListen(this.ui.btn_test_report, this.onTestErrorReport, this);
        UiManager.addButtonListen(this.ui.btn_finishTarget, this.onFinishGameCallback, this);
        UiManager.addButtonListen(this.ui.btn_set, this.onSetCallback, this);
        UiManager.addButtonListen(this.ui.ani_arrow, this.onAniArrowCallback, this);
        UiManager.addButtonListen(this.ui.ani_gun, this.onAniGunCallback, this);
        UiManager.addButtonListen(this.ui.ani_hammer, this.onAniHammeCallback, this);
        this.ui.skillTouchNode.on(cc.Node.EventType.TOUCH_START, this.onSkillUse, this);
        UiManager.addButtonListen(this.ui.hb_node, this.onHbCallback, this);
    };
    public onHbCallback() {
        _.showManageViewToast("闯关成功即可领取", .8, true, cc.v3(0, -cc.winSize.height / 2 + 250));
    };
    public addEventListen() {
        d.listen(h.UP_SKILL, this.updateSkills, this);
        d.listen(h.SHOW_SUCCESS_PAGE, this.showSuccessPage, this);
        d.listen(h.ADD_STEP, this.onAddStep, this);
        d.listen(h.UPDATE_CASH, this.updateCash, this);
        d.listen(h.ON_TARGET_FINISH, this.onFinishTarget, this);
        d.listen(h.UPDATE_AFTER_GAME_CASH, this.updateAfterGameCash, this);
        d.listen(h.NO_STEP, this.onNoStep, this);
        d.listen(h.HIDE_MATCH_LOADINGPAGE, this.playOnloadAni, this);
        d.listen(h.ELIMINATE_FINISH, this.onElimateFinish, this);
        d.listen(h.GAME_CENTER_INIT_FINISH, this.onMatchGameCenterInitFinish, this);
        d.listen(h.ONE_TARGET_FINISH, this.onOneFinishTarget, this);
        d.listen(h.MATCHGAME_FAIL, this.onGameFail, this);
        d.listen(h.FLY_ONE_TARGET, this.playUptargetAni, this);
        d.listen(h.SET_FIRST_SKILL, this.setFirstToushSkill, this);
        d.listen(h.HIDE_SKILL_USE_PAGE, this.onHideSkillPage, this);
    };
    public removeEventListen() {
        d.ignore(h.UP_SKILL, this.updateSkills, this);
        d.ignore(h.SHOW_SUCCESS_PAGE, this.showSuccessPage, this);
        d.ignore(h.ON_TARGET_FINISH, this.onFinishTarget, this);
        d.ignore(h.UPDATE_CASH, this.updateCash, this);
        d.ignore(h.ADD_STEP, this.onAddStep, this);
        d.ignore(h.UPDATE_AFTER_GAME_CASH, this.updateAfterGameCash, this);
        d.ignore(h.NO_STEP, this.onNoStep, this);
        d.ignore(h.HIDE_MATCH_LOADINGPAGE, this.playOnloadAni, this);
        d.ignore(h.ELIMINATE_FINISH, this.onElimateFinish, this);
        d.ignore(h.GAME_CENTER_INIT_FINISH, this.onMatchGameCenterInitFinish, this);
        d.ignore(h.ONE_TARGET_FINISH, this.onOneFinishTarget, this);
        d.ignore(h.MATCHGAME_FAIL, this.onGameFail, this);
        d.ignore(h.FLY_ONE_TARGET, this.playUptargetAni, this);
        d.ignore(h.SET_FIRST_SKILL, this.setFirstToushSkill, this);
        d.ignore(h.HIDE_SKILL_USE_PAGE, this.onHideSkillPage, this);
        this.ui.skillTouchNode.off(cc.Node.EventType.TOUCH_START, this.onSkillUse, this);
    };
    public onDestroy() {
        this.removeEventListen();
    };
    public resetData() {
        this.targetItems = [];
        this.levelInfo = null;
        this.matchGameCenterInitFinished = false;
        this.druingloadAni = true;
        this.resetreadySkillType();
        this.unscheduleAllCallbacks();
        D._instance && D._instance.resetData();
        b.setEnableMultiTouch(true);
    };
    public resetNodesLayers() {
        this.ui.game_bg.zIndex = 0;
        this.ui.centerMask.zIndex = 10;
        this.ui.eliminateBlock.zIndex = 15;
        this.ui.top_bg.zIndex = 20;
        this.ui.bottomNode.zIndex = 30;
        this.ui.skillUsePage.zIndex = 40;
        this.ui.skillTouchNode.zIndex = 50;
        this.ui.targetEffectParent.zIndex = 60;
        this.ui.gameEndNode.zIndex = 70;
        this.ui.flyBoxParent.zIndex = 999;
    };
    public setSkillLayer() {
        this.ui.game_bg.zIndex = 0;
        this.ui.top_bg.zIndex = 10;
        this.ui.bottomNode.zIndex = 20;
        this.ui.skillUsePage.zIndex = 30;
        this.ui.centerMask.zIndex = 40;
        this.ui.eliminateBlock.zIndex = 45;
        this.ui.skillTouchNode.zIndex = 50;
        this.ui.targetEffectParent.zIndex = 60;
        this.ui.gameEndNode.zIndex = 70;
    };
    public updateCash() {
        var e = b.getCurLevelCash() + b.getFlyBoxCash();
        this.ui.lab_coin.getComponent(cc.Label).string = e + "个";
        this.ui.sp_redpack.getComponent(sp.Skeleton).setAnimation(0, "idle_1", false);
    };
    public updateFinalCash(e) {
        f.error("updateFinalCash", e);
        this.ui.lab_coin.getComponent(cc.Label).string = e + "个";
        if ("idle_3" !== this.ui.sp_redpack.getComponent(sp.Skeleton).animation) {
            this.ui.sp_redpack.getComponent(sp.Skeleton).setAnimation(0, "idle_4", false);
            this.ui.sp_redpack.getComponent(sp.Skeleton).setAnimation(1, "idle_3", false);
        }
    };
    public updateAfterGameCash() {
        var e = b.getFinalCoin(false);
        f.error("updateAfterGameCash", e);
        this.ui.lab_coin.getComponent(cc.Label).string = e + "个";
        this.ui.sp_redpack.getComponent(sp.Skeleton).setAnimation(0, "idle_4", false);
    };
    public loadLevelInfo(e) {
        this.resetData();
        if (b.isEditor) {
            b.setLocalGameData(e);
            this.levelInfo = b.getGameInfo();
        } else this.levelInfo = b.getGameInfo();
        b.isEditor && b.setPropInfo({
            2: 99,
            3: 99,
            4: 99
        });
        this.initGameCenter(this.levelInfo);
        this.resetNodesLayers();
    };
    public onEnable() { };
    public initData() { };
    public initGameData(e) {
        var t = this;
        if (b.isEditor) {
            this.playOnloadAni();
            e ? this.loadLevelInfo(e) : cc.loader.loadRes("levelConfig/map_11", cc.JsonAsset, function (e, a) {
                if (e) f.log("加载关卡报错", e); else {
                    f.log("loadRes json---", a);
                    var o = a.json;
                    t.loadLevelInfo(o);
                }
            });
        } else {
            this.loadLevelInfo(null);
            b.setEnableMultiTouch(false);
        }
    };
    public blockEliminate(e) {
        this.ui.eliminateBlock.active = e;
        b.gameStatus === MatchGameStatus.DuringGame ? b.left_step <= 0 ? this.ui.eliminateBlock.active = true : P.getLightBallCount() > 0 && (this.ui.eliminateBlock.active = true) : this.ui.eliminateBlock.active = true;
    };
    public getEliminateActive() {
        return this.ui.eliminateBlock.active;
    };
    public playOnloadAni() {
        var e = this;
        this.druingloadAni = true;
        this.node.getComponent(cc.Widget).enabled = false;
        this.ui.centerMask.x = 720;
        M.playEnterBoardAudio();
        cc.tween(this.ui.centerMask).to(0.01, {
            x: 0
        }, {
            easing: "backOut"
        }).call(function () {
            e.druingloadAni = false;
            e.node.getComponent(cc.Widget).enabled = true;
            p.dealPlayBgMusic(true);
            e.onLoadAniFinish();
        }).start();
    };
    public onLoadAniFinish() {
        this.druingloadAni = false;
        this.matchGameCenterInitFinished && this.showLevelGuide();
    };
    public onMatchGameCenterInitFinish() {
        this.matchGameCenterInitFinished = true;
        this.druingloadAni || this.showLevelGuide();
    };
    public showLevelGuide() {
        var e = this, t = b.guideIndex;
        if (t > 0) this.scheduleOnce(function () {
            e.showGuidePage(t);
        }, .1); else {
            b.duringGuide = false;
            D._instance.setDuringGameStatu();
            this.blockEliminate(false);
            P.getMatchGuideData();
        }
    };
    public showGuidePage(e) {
        var t = C.getLevelGuideInfo(e);
        f.log("关卡数据", JSON.stringify(t));
        t && t.length && w.showPage("MatchGameGuidePage", {
            guideInfoArr: t
        });
        D._instance.setDuringGameStatu();
        this.blockEliminate(false);
    };
    public initGameCenter(e) {
        this.ui.centerMask.removeAllChildren();
        this.isFinish = false;
        this.setMatchGameCenterActive(true);
        UiManager.loaderViewDeepPath(this.ui.centerMask, "MatchGameCenter", "MatchGame/view", e, 10);
        this.initTop();
        this.initBottom();
        this.initGameBg();
        this.setGameEndNodeShow(false);
        this.ui.gameFailNode.active = false;
        this.ui.ChuangKuanShiBai.active = false;
        this.initPanda();
        var t = b._gameLevel;
        m.reportData("matchgame_game_start", {
            level: t
        });
        b.initStartGameTime();
        this.ui.flyBoxToastNode.active = false;
        this.resetFlyBox();
    };
    public initGameBg() {
        var e = this, t = p.getCurryCityBg();
        UiManager.loadSpriteFrameInDeepPath(e.ui.game_bg.getComponent(cc.Sprite), "imgs/cityBg/bg_1");//暂时去掉
        return;
        var e = this, t = p.getCurryCityBg();
        if (t) this.ui.game_bg.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(t); else {
            var a = p.getCurrentLocation();
            if (null == a) {
                UiManager.loadSpriteFrameInDeepPath(this.ui.game_bg.getComponent(cc.Sprite), "imgs/cityBg/bg_1");
                return;
            }
            var o = p.getRemoteUrlStr() + a + ".png";
            _.loadRemoteImg(o).then(function (t) {
                f.log("到达新城市游戏内===当前没有获取到现在加载====成功");
                e.ui.game_bg.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(t);
            }).catch(function () {
                UiManager.loadSpriteFrameInDeepPath(e.ui.game_bg.getComponent(cc.Sprite), "imgs/cityBg/bg_1");
                f.log("到达新城市游戏内===当前没有获取到现在加载====失败");
            });
        }
    };
    public initTop() {
        b.left_step = this.levelInfo.totalStep;
        var e = b.levelCategory == T.GameLevelType.GoldCoin ? "imgs/game/matchMain/main/reward_bg" : "imgs/game/matchMain/main/target_bg";
        UiManager.loadSpriteFrameInDeepPath(this.ui.target_bg_2.getComponent(cc.Sprite), e);
        this.updateStep();
        this.ui.notice_bg.active = false;
    };
    public showNotice(e?) {
        var t = this;
        void 0 === e && (e = false);
        if (!b.isEditor) {
            this.ui.notice_bg.active = true;
            this.ui.rich_next_city_tip.getComponent(cc.RichText).string = b.getNextCityTipString();
            e || this.scheduleOnce(function () {
                t.ui.notice_bg.active = false;
            }, 4);
        }
    };
    public async initTarget(): Promise<void> {
        if (!this.targetItemPrefab) {
            this.targetItemPrefab = await UiManager.loaderPrefabInDeepPath("prefabs/targetItemPrefab1");
            g.initPool(this.targetItemPrefab, 0, "target_item_pool");
        }

        const t = b.target_info;
        if (t) {
            this.ui.target_item_parent.removeAllChildren();
            this.targetItems = [];

            for (let i = 0; i < t.length; i++) {
                const e = g.getNode("target_item_pool");
                this.targetItems.push(e);
                this.ui.target_item_parent.addChild(e);
                e.name = "target_item_" + i;
                const a = t[i];
                const r = b.getTargetImgPath(a);
                await UiManager.loadSpriteFrameInDeepPath(e.getChildByName("img_target").getComponent(cc.Sprite), r);

                await new Promise<void>((resolve) => {
                    this.scheduleOnce(() => {
                        const a = e.getChildByName("img_target");
                        const t = a.height > a.width ? 70 / a.height : 70 / a.width;
                        a.scale = t;
                        resolve();
                    });
                });

                e.getChildByName("target_count").getComponent(cc.Label).string = a.tootalCount;
                e.getChildByName("target_finish_spine").active = false;
                e.getChildByName("update_spine").active = false;
            }
        }

        this.setTargetPos();
    }

    public setTargetPos() {
        if (1 == this.targetItems.length) {
            this.targetItems[0].position = cc.v3();
            this.targetItems[0].scale = 1.1;
        } else if (2 == this.targetItems.length) {
            this.targetItems[0].position = cc.v3(-40);
            this.targetItems[1].position = cc.v3(40);
            this.targetItems[0].scale = 1;
            this.targetItems[1].scale = 1;
        } else if (3 == this.targetItems.length) {
            this.targetItems[0].position = cc.v3(-80);
            this.targetItems[1].position = cc.v3(0);
            this.targetItems[2].position = cc.v3(80);
            this.targetItems[0].scale = 1;
            this.targetItems[1].scale = 1;
            this.targetItems[2].scale = 1;
        } else if (4 == this.targetItems.length) {
            this.targetItems[0].position = cc.v3(-100);
            this.targetItems[1].position = cc.v3(-20);
            this.targetItems[2].position = cc.v3(60);
            this.targetItems[3].position = cc.v3(140);
            this.targetItems[0].scale = 1;
            this.targetItems[1].scale = 1;
            this.targetItems[2].scale = 1;
            this.targetItems[3].scale = 1;
        }
    };
    public updateTarget() {
        for (var e = 0; e < this.targetItems.length; e++) if (!b.target_info[e].isFinish) {
            this.targetItems[e].getChildByName("target_count").active = true;
            this.targetItems[e].getChildByName("target_count").getComponent(cc.Label).string = "" + b.target_info[e].tootalCount;
            this.targetItems[e].getChildByName("target_finish_spine").active = false;
        }
    };
    public playUptargetAni(e) {
        var t = e.index;
        if (this.targetItems && this.targetItems[t]) {
            var a = this.targetItems[t].getChildByName("update_spine");
            a.active = true;
            var o = a.getComponent(sp.Skeleton);
            o.setAnimation(0, "break1", false);
            o.setCompleteListener(function () {
                o.setCompleteListener(null);
                a.active = false;
            });
        }
    };
    public updateStep() {
        var e = b.left_step;
        this.ui.lab_step.getComponent(cc.Label).string = "" + e;
        e <= 0 && (this.ui.lab_step.getComponent(cc.Label).string = "0");
        var t = b.total_step - b.left_step;
        c.getIsGncd() && b.gameStatus == MatchGameStatus.DuringGame && !b.startReplaceStep && t % 5 == 0 && 0 !== b.left_step && this.createFlyBox(t);
        20 != b.gameLevel ? 3 != t && 8 != t && 15 != t && 25 != t || b.gameStatus != MatchGameStatus.DuringGame || b.startReplaceStep || this.showNotice() : t >= 1 && b.gameStatus == MatchGameStatus.DuringGame ? this.showNotice(true) : this.ui.notice_bg.active = false;
    };
    public initBottom() {
        this.ui.bottomNode.y = -cc.winSize.height / 2 + 70;
        this.initSkills();
        this.resetreadySkillType();
        this.updateCash();
    };
    public setNoticeParentPos() {
        p.setRollingNoticeParent(this.ui.noticeParent);
        // var e = _.getNodeAToNodeBPoint(this.ui.centerMask.getChildByName("MatchGameCenter"), this.ui.noticeParent);
        // this.ui.noticeParent.y = e.y + 50;
    };
    public updateSkills() {
        var e = this;
        b.propInfo.forEach(function (t) {
            e.setSkills(t);
        });
    };
    public getSkillBtn(e) {
        return this.ui["btn_" + b.getSkillName(e)];
    };
    public initSkills() {
        var e = this;
        b.propInfo.forEach(function (t) {
            e.initOneSkill(t);
        });
    };
    public getSkillImg(e) {
        return this.ui["img_" + b.getSkillName(e)];
    };
    public initOneSkill(e) {
        var t = e.skillType, a = e.skillCount, o = "imgs/game/matchMain/btn_" + b.getSkillName(t);
        if (6 == b.guideIndex && t == SkillType.Hammer || 7 == b.guideIndex && t == SkillType.Arrow || 8 == b.guideIndex && t == SkillType.Gun) {
            this.getSkillLab(t).parent.active = false;
            UiManager.loadSpriteFrameInDeepPath(this.getSkillImg(t).getComponent(cc.Sprite), o);
        } else {
            UiManager.loadSpriteFrameInDeepPath(this.getSkillImg(t).getComponent(cc.Sprite), a >= 0 ? o : "imgs/game/matchMain/btn_clock");
            this.getSkillLab(t).parent.active = a > 0;
            this.getSkillLab(t).getComponent(cc.Label).string = "" + a;
        }
        this.getSkillImg(t).active = a >= 0;
    };
    public setSkills(e) {
        var t = e.skillType, a = e.skillCount, o = "imgs/game/matchMain/btn_" + b.getSkillName(t);
        if (b.duringGuide && (6 == b.guideIndex && t == SkillType.Hammer || 7 == b.guideIndex && t == SkillType.Arrow || 8 == b.guideIndex && t == SkillType.Gun)) {
            this.getSkillLab(t).parent.active = false;
            UiManager.loadSpriteFrameInDeepPath(this.getSkillImg(t).getComponent(cc.Sprite), o);
        } else {
            UiManager.loadSpriteFrameInDeepPath(this.getSkillImg(t).getComponent(cc.Sprite), a >= 0 ? o : "imgs/game/matchMain/btn_clock");
            this.getSkillLab(t).parent.active = a > 0;
            this.getSkillLab(t).getComponent(cc.Label).string = "" + a;
        }
        this.getSkillImg(t).active = a >= 0;
    };
    public getSkillLab(e) {
        return this.ui[b.getSkillName(e) + "_lab"];
    };
    public resetreadySkillType() {
        this.readySkillType = SkillType.Null;
        this.ui.skillUsePage.active = false;
        this.ui.skillTouchNode.active = false;
        this.resetNodesLayers();
    };
    public onHammer() {
        var e = b.propInfo[0];
        if (e) {
            var t = (e.skillType, e.skillCount);
            t < 0 || (0 == t ? this.showAddSkillPage(SkillType.Hammer) : this.setFirstToushSkill(SkillType.Hammer));
        }
    };
    public onArrow() {
        var e = b.propInfo[1]
        if (e) {
            var t = (e.skillType, e.skillCount);
            t < 0 || (0 == t ? this.showAddSkillPage(SkillType.Arrow) : this.setFirstToushSkill(SkillType.Arrow));
        }
    };
    public onGun() {
        var e = b.propInfo[2]
        if (e) {
            var t = (e.skillType, e.skillCount);
            t < 0 || (0 == t ? this.showAddSkillPage(SkillType.Gun) : this.setFirstToushSkill(SkillType.Gun));
        }
    };
    public onShiff() {
        var e = b.propInfo[3], t = (e.skillType, e.skillCount);
        if (t < 0) { } else if (0 == t) this.showAddSkillPage(SkillType.Shiff); else {
            this.readySkillType = SkillType.Shiff;
            this.onFirstTouchSkill();
        }
    };
    public showAddSkillPage(e) {
        b.isEditor || w.showPage("MatchGameAddPropPage", {
            type: e
        });
    };
    public onAniArrowCallback() { };
    public onAniGunCallback() { };
    public onAniHammeCallback() { };
    public setFirstToushSkill(e) {
        this.readySkillType = e;
        this.onFirstTouchSkill();
    };
    public onFirstTouchSkill() {
        this.ui.skillUsePage.active = true;
        this.ui.skillTouchNode.active = true;
        this.setSkillLayer();
        this.setUseSkillAni();
    };
    public onHideSkillPage() {
        this.ui.skillUsePage.active = false;
    };
    public setUseSkillAni() {
        this.ui.ani_arrow.active = false;
        this.ui.ani_gun.active = false;
        this.ui.ani_hammer.active = false;
        switch (this.readySkillType) {
            case SkillType.Arrow:
                this.ui.ani_arrow.active = true;
                break;

            case SkillType.Gun:
                this.ui.ani_gun.active = true;
                break;

            case SkillType.Hammer:
                this.ui.ani_hammer.active = true;
        }
        var e = "imgs/game/matchMain/skill_" + this.readySkillType;
        UiManager.loadSpriteFrameInDeepPath(this.ui.img_skill_tip.getComponent(cc.Sprite), e);
    };
    public useSkill(e) {
        var t = this;
        f.log("使用道具", e);
        this.readySkillType < 0 || this.getSkillCanUse(e, this.readySkillType) && A.useProp(this.readySkillType, function () {
            var a, o, i = e.row, r = e.col;
            switch (t.readySkillType) {
                case SkillType.Arrow:
                    S.useArrow({
                        row: e.row,
                        col: e.col
                    });
                    break;

                case SkillType.Gun:
                    S.useGun({
                        row: e.row,
                        col: e.col
                    });
                    t.playBottomHideAni();
                    break;

                case SkillType.Hammer:
                    o = r;
                    a = i;
                    G.playHammer({
                        row: a,
                        col: o
                    }, function () {
                        S.useHammer({
                            row: a,
                            col: o
                        });
                    });
            }
            t.resetreadySkillType();
        });
    };
    public getSkillCanUse(e, t) {
        return P.getSkillCanUse(e, t);
    };
    public onSkillUse(e) {
        if (cc.isValid(D._instance.node)) {
            let d = e.getLocation();
            d.x += 38;
            d.y -= 38;
            var t = D._instance.node.convertToNodeSpaceAR(d), a = this.getRowColByPos(cc.v2(t));
            if (!a) {
                this.resetreadySkillType();
                return;
            }
            if (a) {
                var o = a.row, i = a.col;
                if (D._instance.displayMapInfo[o][i] !== T.DisplayMapType.Normal) {
                    this.resetreadySkillType();
                    return;
                }
                this.useSkill(a);
            }
        }
    };
    public getRowColByPos(e) {
        var t, a, o = e.x, i = e.y;
        t = Math.floor(o / MatchGameConfig.ITEM_WIDTH);
        a = Math.floor(-i / MatchGameConfig.ITEM_HEIGHT);
        return t >= 0 && t <= T.MAX_ROW && a >= 0 && a <= T.MAX_COL && D._instance.displayMapInfo[t][a] ? {
            row: t,
            col: a
        } : null;
    };
    public onSet() {
        this.experieceMap();
    };
    public experieceMap() {
        var e = this;
        cc.assetManager.loadRemote("https://static.dayclover.com/wx_game/feie/apkLevelConfig/experienceMap.json", function (t, a) {
            if (t) cc.error(t.message || t); else if (a) {
                var o = a['json'];
                if (o) {
                    e.initGameData(o);
                    f.log("加载完成========的json数据", a['json']);
                }
            }
        });
    };
    public showSuccessPage(e) {
        if (!this.isFinish) {
            this.isFinish = true;
            this.ui.skipTip.active = false;
            b.levelCategory != T.GameLevelType.GoldCoin ? this.openSuccessPage({
                strtFlyNode: this.ui.sp_redpack,
                step: e
            }) : this.playTargetCoinCollectRedPackAni(e);
        }
    };
    public playTargetCoinCollectRedPackAni(e) {
        var t = this, a = b.target_info[0].tootalCount, o = e, i = b.total_step + b.video_step;
        this.playCoinStrChangeAni(a, 0, 1.2, 20, this.targetItems[0].getChildByName("target_count").getComponent(cc.Label), false, function () {
            t.playCoinStrChangeAni2(i, o, 1.2, 20, t.ui.lab_coin.getComponent(cc.Label), true, function () {
                t.openSuccessPage({
                    strtFlyNode: t.ui.sp_redpack,
                    step: e
                });
            });
        });
        for (var r = 0; r < 20; r++) k.playTargetCoinToRedPackAni(this.ui.target_item_parent, this.ui.hb_node, null);
    };
    public openSuccessPage(e) {
        this.dealOutSideGuide();
        c.getBtlj2Ab() ? w.showPage("MatchGameSuccessSettlementTwoPage", e) : c.getIsJsAb() ? w.showPage("MatchGameSuccessSettlementPage", e) : c.isOptimizeTravelFundFlag() ? w.showPage("MatchGameSuccessJJPage", e) : w.showPage("MatchGameSuccessPage", e);
    };
    public dealOutSideGuide() {
        1 == b.gameLevel && O.reportOutsideGuideStep(u.OutSideLevelGuideExperimentStep.BREAKTHROUGH);
    };
    public playCoinStrChangeAni(e, t, a, o, i, r, n) {
        void 0 === o && (o = 20);
        var l = -1, s = e - t;
        this.schedule(function () {
            if (++l < o) i.string = r ? MathUtils.accDiv(Math.floor(e - l / o * s), 100) + "元" : "" + Math.floor(e - l / o * s); else {
                i.string = r ? MathUtils.accDiv(t, 100) + "元" : "" + t;
                n && n();
            }
        }, a / o, o);
    };
    public playCoinStrChangeAni2(e, t, a, o, i, r, n) {
        void 0 === o && (o = 20);
        var l = -1, s = e - t;
        this.schedule(function () {
            if (++l < o) if (r) {
                var a = Math.floor(e - l / o * s);
                i.string = a + "g";
            } else {
                a = Math.floor(e - l / o * s);
                i.string = a + "g";
            } else {
                if (r) {
                    a = t;
                    i.string = a + "g";
                } else {
                    a = t;
                    i.string = a + "g";
                }
                n && n();
            }
        }, a / o, o);
    };
    public showFailPage() {
        w.showPage("MatchGameFailPage", null);

    };
    public onTestFail() {
        this.onNoStep();
    };
    public onTestSuccess() {
        this.showSuccessPage(b.total_step);
    };
    public onTestErrorReport() {
        // var e = reportErrorQueue.getItems();
        // m.reportData("report_game_error", {
        //     error_message: e
        // });
        // m.showToast("上传成功");
    };
    public onOneFinishTarget(e) {
        if (e) {
            var t = e.index, a = this.targetItems[t];
            if (a) {
                b.target_info;
                if (b.target_info) {
                    this.playTargetItemShowFinish(a);
                    this.playUptargetAni(e);
                }
            }
        }
    };
    public playTargetItemShowFinish(e) {
        if (b.levelCategory != T.GameLevelType.GoldCoin) {
            e.getChildByName("target_count").active = false;
            e.getChildByName("target_finish_spine").active = true;
            var t = e.getChildByName("target_finish_spine").getComponent(sp.Skeleton);
            t.setAnimation(0, "idle_1", false);
            t.setAnimation(1, "idle_2", false);
        }
    };
    public onFinishTarget() {
        var e = this;
        m.reportData("matchgame_show_target_finish", {
            level: b.gameLevel,
            step: b.left_step,
            all_step: b.total_step
        });
        this.setGameEndNodeShow(true);
        this.setGameSuccesssShow(true);
        M.playTargetFinishAudio();
        this.setPandavictoryStatu();
        this.ui.MuBiaoDaCheng.active = true;
        this.ui.skipTip.active = false;
        this.ui.MuBiaoDaCheng.getComponent(sp.Skeleton).setAnimation(0, "idle_1", false);
        this.ui.MuBiaoDaCheng.getComponent(sp.Skeleton).setCompleteListener(function () {
            e.ui.MuBiaoDaCheng.active = false;
            b.levelCategory !== T.GameLevelType.GoldCoin && (e.ui.skipTip.active = true);
            P.onGameSuccess();
        });
    };
    public onFinishGameCallback() {
        if (0 != this.ui.skipTip.active) {
            this.ui.skipTip.active = false;
            if (!(b.gameLevel < 0)) {
                m.reportData("matchgame_click_target_skip", {
                    level: b.gameLevel
                });
                b.haveShowEndGamePage = true;
                b.setEndGameCash(b.endGamePropCount);
                var e = b.getFinalCoin(true);
                this.showSuccessPage(e);
            }
        }
    };
    public onSetCallback() {
        b.duringGuide || w.showPage("MatchGameSetPage", null);
    };
    public onNoStep() {
        var e = this;
        if (!this.ui.gameEndNode.active || !this.ui.sp_no_step.active) {
            this.ui.sp_no_step.getComponent(sp.Skeleton).clearTracks();
            this.setGameEndNodeShow(true);
            this.setGameSuccesssShow(false);
            this.setPandaFailStatu();
            this.ui.sp_no_step.active = true;
            M.playNoStepShowAudio();
            this.ui.sp_no_step.getComponent(sp.Skeleton).setAnimation(0, "idle_1", false);
            this.ui.sp_no_step.getComponent(sp.Skeleton).setAnimation(1, "idle_2", false);
            this.scheduleOnce(function () {
                e.ui.sp_no_step.active = false;
                e.showFailPage();
                M.playNoStepExitAudio();
            }, 2);
        }
    };
    public onGameFail() {
        var e = this;
        if (!this.ui.gameFailNode.active || !this.ui.ChuangKuanShiBai.active) {
            M.playNoStepShowAudio();
            this.ui.ChuangKuanShiBai.getComponent(sp.Skeleton).clearTracks();
            this.setGameEndNodeShow(true);
            this.ui.gameSuccessNode.active = false;
            this.ui.gameNoStepNode.active = false;
            this.ui.gameFailNode.active = true;
            this.ui.ChuangKuanShiBai.active = true;
            this.setPandaFailStatu();
            this.ui.ChuangKuanShiBai.active = true;
            this.ui.ChuangKuanShiBai.getComponent(sp.Skeleton).setAnimation(0, "idle_1", false);
            this.ui.ChuangKuanShiBai.getComponent(sp.Skeleton).setAnimation(1, "idle_2", false);
            this.scheduleOnce(function () {
                e.ui.ChuangKuanShiBai.active = false;
                e.ui.gameFailNode.active = false;
                M.playFailExitAudio();
                m.reportData("matchgame_fail_exit", {
                    level: b.gameLevel,
                    game_time: b.getGameTime()
                });
                d.trigger(h.END_MATCH_GAME, {
                    is_tg: 0
                });
            }, 2);
        }
    };
    public setGameEndNodeShow(e) {
        this.ui.gameEndNode.active = e;
    };
    public setGameSuccesssShow(e) {
        if (e && b.gameLevel >= 0) {
            this.ui.skipTip.active = b.levelCategory !== T.GameLevelType.GoldCoin;
            p.setRollingNoticeOpacity(0);
        } else {
            this.ui.skipTip.active = false;
            p.setRollingNoticeOpacity(255);
        }
        this.ui.gameSuccessNode.active = e;
        this.ui.gameNoStepNode.active = !e;
        this.ui.gameFailNode.active = false;
        this.ui.ChuangKuanShiBai.active = false;
    };
    public onAddStep() {
        this.setGameEndNodeShow(false);
        this.updateStep();
        D._instance.setDuringGameStatu();
        this.setPandaIdleStatu();
        D._instance.setDuringGameStatu();
        this.blockEliminate(false);
        this.onElimateFinish();
        p.setRollingNoticeOpacity(255);
    };
    public playPandaAni() { };
    public initPanda() {
        var e = this;
        this.setPandaIdleStatu();
        var t = cc.isValid(this.ui.panda_1) && this.ui.panda_1.getComponent(sp.Skeleton);
        t && t.setCompleteListener(function () {
            e.onPandaSpCompleteListener();
        });
    };
    public onPandaSpCompleteListener() {
        var e = cc.isValid(this.ui.panda_1) && this.ui.panda_1.getComponent(sp.Skeleton);
        if (e) {
            var t = e.animation;
            if (this.pandaStatu == s.thinking) {
                if ("think" == t) {
                    e.setAnimation(0, "idle_3", false);
                    e.addAnimation(0, "idle_1", false);
                    e.addAnimation(0, "think", false);
                }
            } else this.pandaStatu == s.Idle ? this.setPandaIdleStatu() : this.pandaStatu == s.Elimate ? "idle_4" == t && this.setPandaIdleStatu() : this.pandaStatu == s.victory ? "victory" == t && this.setPandaIdleStatu() : (this.pandaStatu,
                s.Fail);
        }
    };
    public setPandaFailStatu() {
        this.pandaStatu = s.Fail;
        var e = cc.isValid(this.ui.panda_1) && this.ui.panda_1.getComponent(sp.Skeleton);
        e && e.setAnimation(0, "unhappy", true);
    };
    public setPandavictoryStatu() {
        if (this.pandaStatu != s.victory) {
            this.pandaStatu = s.victory;
            var e = cc.isValid(this.ui.panda_1) && this.ui.panda_1.getComponent(sp.Skeleton);
            e && e.setAnimation(0, "victory", false);
        }
    };
    public setPandaIdleStatu() {
        this.pandaStatu = s.Idle;
        if (this.ui.panda_1) try {
            var e = cc.isValid(this.ui.panda_1) && this.ui.panda_1.getComponent(sp.Skeleton);
            e && e.setAnimation(0, "idle_1", true);
        } catch (e) { }
    };
    public setPandathinkStatus() {
        if (b.gameStatus === MatchGameStatus.DuringGame && this.pandaStatu != s.thinking) {
            this.pandaStatu = s.thinking;
            var e = cc.isValid(this.ui.panda_1) && this.ui.panda_1.getComponent(sp.Skeleton);
            if (e) {
                e.setAnimation(0, "idle_3", false);
                e.addAnimation(0, "idle_1", false);
                e.addAnimation(0, "think", false);
            }
        }
    };
    public setPandaElimateStatus() {
        if (b.gameStatus === MatchGameStatus.DuringGame && this.pandaStatu != s.Elimate) {
            this.pandaStatu = s.Elimate;
            var e = cc.isValid(this.ui.panda_1) && this.ui.panda_1.getComponent(sp.Skeleton);
            if (e) {
                e.setAnimation(0, "idle_2", false);
                e.addAnimation(0, "idle_4", false);
            }
        }
    };
    public onElimateFinish() {
        this.unschedule(this.setPandathinkStatus);
        this.scheduleOnce(this.setPandathinkStatus, 3);
    };
    public setMatchGameCenterActive(e) {
        this.ui.centerMask.opacity = e ? 255 : 0;
        this.ui.targetEffectParent.opacity = e ? 255 : 0;
        b.skipEndGameReplacement = !e;
        e && p.setRollingNoticeParent(this.ui.noticeParent);
    };
    public playBottomHideAni() {
        cc.tween(this.ui.bottomMoveNodes).delay(.5).by(.5, {
            y: -300
        }, {
            easing: "backInOut"
        }).delay(.5).to(.7, {
            y: 0
        }, {
            easing: "backInOut"
        }).start();
    };
    public async createFlyBox(e: number): Promise<void> {
        m.reportData("show_matchgame_fly_box", { step: e });

        if (!this.flyBoxPrefab) {
            this.flyBoxPrefab = await UiManager.loaderPrefabInDeepPath("prefabs/flyBoxPrefab");
            g.initPool(this.flyBoxPrefab, 0, T.SquarePool.FlyBoxItem);
        }

        const flyBoxItem = g.getNode(T.SquarePool.FlyBoxItem);
        flyBoxItem.parent = this.ui.flyBoxParent;
        flyBoxItem.off(cc.Node.EventType.TOUCH_END, this.onTouchFlyBox, this);
        flyBoxItem.on(cc.Node.EventType.TOUCH_END, this.onTouchFlyBox, this);
        this.playFlyBoxAni(flyBoxItem);
    }
    public playFlyBoxAni(e) {
        e.y = 0;
        e.x = -cc.winSize.width / 2 - e.width;
        var t = cc.winSize.width / 2 + e.width;
        cc.tween(e).to(10, {
            x: t
        }).call(function () {
            e.stopAllActions();
            g.putNode(T.SquarePool.FlyBoxItem, e);
        }).start();
        cc.tween(e).repeatForever(cc.tween(e).by(.8, {
            y: 100
        }).by(.8, {
            y: -100
        })).start();
    };
    public onTouchFlyBox(e) {
        var t = this;
        f.log("onTouchFlyBox");
        m.reportData("matchgame_fly_box_touch");
        e.target.pauseAllActions();
        m.showForceToast("看完广告就能获得大量红包");
        b.onPlayFlyBoxVd();
        y.getInstance().playNormalVideoAd(function () {
            A.addRewardVideo(function () {
                p.reportVideoBuriedPoint(VideoBuriedPoint.FLY_BOX);
                b.onFlyBoxVdFinish();
                g.putNode(T.SquarePool.FlyBoxItem, e.target);
                t.showFlyBoxToast(b.getCurFlyBoxCash());
                for (var a = 0; a < 20; a++) k.playFlyBoxToRedPackAni(t.ui.flyBoxParent, t.ui.hb_node, null);
                t.scheduleOnce(function () {
                    b.gameStatus == MatchGameStatus.DuringGame ? d.trigger(h.UPDATE_CASH) : d.trigger(h.UPDATE_AFTER_GAME_CASH);
                }, 1);
            });
        }, function () {
            e.target.resumeAllActions();
        });
    };
    public showFlyBoxToast(e) {
        var t = this;
        this.ui.flyBoxToastNode.active = true;
        this.ui.flyBoxToastNode.opacity = 255;
        this.ui.countLabel.getComponent(cc.Label).string = _.getCashBalance(e) + "";
        cc.tween(this.ui.flyBoxToastNode).delay(2).to(.5, {
            opacity: 0
        }).call(function () {
            t.ui.flyBoxToastNode.active = false;
        }).start();
    };
    public resetFlyBox() {
        this.ui.flyBoxParent.children.forEach((e) => {
            "flyBoxPrefab" == e.name && g.putNode(T.SquarePool.FlyBoxItem, e);
        });
    };
    //             var a;
    // t.prefabUrl = "assets/resources/prefabs/MatchGame/view/MatchGameView";
    // t.className = "MatchGameViewCtrl";
    // return a = r([L, R("UI/view/MatchGameViewCtrl")], t);
}